<?php
class siteblockerclass {

	function siteblockerclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function site_status($status=0){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='site_status'", __LINE__, __FILE__);
		if ($status == 0){
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`, `name`, `value`) VALUES ('', 'site_status', '0')", __LINE__, __FILE__);
		} else {
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`, `name`, `value`) VALUES ('', 'site_status', '1')", __LINE__, __FILE__);
		}
	}
	
	function get_site_status(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='site_status'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) < 1) return 0;
		else {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			return $row['value'];
		}
	}
	
	function get_info(){
		global $treeclass;
		$result = array();
		$tmp = $treeclass->_get_site_settings();
		if (isset($tmp['site_webmaster_email01']) && (strlen($tmp['site_webmaster_email01']) > 0)){
			$result['email'] = $tmp['site_webmaster_email01'];
		} elseif (isset($tmp['site_email01']) && (strlen($tmp['site_email01']) > 0)){
			$result['email'] = $tmp['site_email01'];
		} else $result['email'] = "";
		$tmp = parse_url($this->setupdata['base_href']);
		$result['site'] = $tmp['host'];
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='site_block_text'", __LINE__, __FILE__);		
		if (mysql_num_rows($sql) < 1) $result['text'] = "Page closed!";
		else {			
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$result['text'] = stripslashes($row['value']);
		}
		return $result;
	}
	
	function get_ips(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='site_allow_ip'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) < 1) $result['allow'] = "";
		else {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$result['allow'] = $row['value'];
		}

		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='site_block_ip'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) < 1) $result['block'] = "";
		else {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$result['block'] = $row['value'];
		}

		return $result;
	}
	
	function save_settings($array){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='site_block_text' OR `name`='site_allow_ip' OR `name`='site_block_ip'", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`, `name`, `value`) VALUES ('', 'site_block_text', '".addslashes(trim($array['settings_text']))."'), ('', 'site_allow_ip', '".addslashes(trim($array['allow_ip']))."'), ('', 'site_block_ip', '".addslashes(trim($array['block_ip']))."')", __LINE__, __FILE__);
	}
	
	function is_site_allow(){
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
		else $ip_address = $_SERVER['REMOTE_ADDR'];
		$result = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name` = 'site_allow_ip' AND `value` LIKE '%".$ip_address."%'", __LINE__, __FILE__);
		if (mysql_num_rows($result) > 1) return 0;
		else return 1;
	}

	function import_from_maxmind_file($file){
		$str = "";
		$z = zip_open($file['tmp_name']);
		while ($entry = zip_read($z)) {
			if ((zip_entry_name($entry) == "GeoIPCountryWhois.csv") && (zip_entry_filesize($entry)) && (zip_entry_open($z, $entry))){
				$str .= zip_entry_read($entry,zip_entry_filesize($entry));
			}
		}
		$tmp = explode("\n", $str);
		if (is_array($tmp) && (count($tmp) > 0)){
			$this->mysqlclass->mysql_my_query("TRUNCATE TABLE `".$this->setupdata['table_prefix']."siteblocker_country_ip`", __LINE__, __FILE__);
			foreach($tmp as $k=>$v){
				$v = trim($v);
				$tmp2 = explode(",", $v);
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."siteblocker_country_ip` (`ip_f`,`ip_t`,`ip_nf`,`ip_nt`,`cn`,`country`) VALUES ('".substr($tmp2[0], 1, strlen($tmp2[0])-2)."', '".substr($tmp2[1], 1, strlen($tmp2[1])-2)."', '".substr($tmp2[2], 1, strlen($tmp2[2])-2)."', '".substr($tmp2[3], 1, strlen($tmp2[3])-2)."', '".substr($tmp2[4], 1, strlen($tmp2[4])-2)."', '".addslashes((substr($tmp2[5], 1, strlen($tmp2[5])-2)))."')", __LINE__, __FILE__);
			}
		}
		$this->mysqlclass->close_db();
		include $this->setupdata['core_dir']."/includes/die.php";
	}
}
?>
